package com.sisgesbyte.dao.seguridad;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.sisgesbyte.dao.beanDao.RolDetRequest;
import com.sisgesbyte.data.entity.comun.GenericEntity;
import com.sisgesbyte.data.entity.seguridad.RolListaEntity;
import com.sisgesbyte.data.util.AbstractFacade;

@Stateless
public class MantenimientoRolDaoImpl extends AbstractFacade<RolListaEntity> implements MantenimientoRolDao{

	@PersistenceContext(unitName = "SigesSiscompPU")
	private EntityManager em;

	@Override
	protected EntityManager getEntityManager() {
		return em;
	}
	
	public MantenimientoRolDaoImpl(){
		super(RolListaEntity.class);
	}

	@Override
	public List<RolListaEntity> listaRol() {
		
		StringBuilder jpql = new StringBuilder();
			jpql.append("select id, nombre, active from seguridad.rol");
			
			Query query = em.createNativeQuery(jpql.toString(),RolListaEntity.class);
		
		List<RolListaEntity> ls = query.getResultList();
		
		return ls;
	}

	@Override
	public GenericEntity guardarRol(RolDetRequest request) {
		
		StringBuilder jpql = new StringBuilder();
			jpql.append("select * from seguridad.sp_registrar_rol (:in_nombre, :in_active)");
		
		Query query = em.createNativeQuery(jpql.toString(), GenericEntity.class);
			query.setParameter("in_nombre", request.getNombreRol());
			query.setParameter("in_active", request.getActive());
			
		return (GenericEntity) query.getSingleResult();
	}

	@Override
	public GenericEntity actualizarRol(RolDetRequest request) {
		StringBuilder jpql = new StringBuilder();
			jpql.append("select * from seguridad.sp_actualizar_rol (:in_codrol, :in_nombre, :in_active)");
		
		Query query = em.createNativeQuery(jpql.toString(), GenericEntity.class);
			query.setParameter("in_codrol", request.getCodRol());
			query.setParameter("in_nombre", request.getNombreRol());
			query.setParameter("in_active", request.getActive());
			
		return (GenericEntity) query.getSingleResult();
	}

	@Override
	public GenericEntity eliminarRol(int codRol) {
		
		StringBuilder jpql = new StringBuilder();
			jpql.append("select * from seguridad.sp_eliminar_rol (:in_codrol)");
	
		Query query = em.createNativeQuery(jpql.toString(), GenericEntity.class);
			query.setParameter("in_codrol", codRol);
		
		return (GenericEntity) query.getSingleResult();
	}

}
